package com.xj.opcua;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.xj.opcua.client.Client;
import com.xj.opcua.client.util.OpcUaClentUtils;
import com.xj.opcua.data.cache.OpcUaDataCache;

@Service
public class GlobalBoostrapService implements ApplicationRunner{
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		ExecutorService es = Executors.newFixedThreadPool(4);
		es.submit(() -> {
			String urlString = "opc.tcp://192.168.1.123:4840";
			JSONObject result = OpcUaClentUtils.connectOpcUaClent(urlString);
			if(result.getInteger("code") == 200) {
				//订阅
				OpcUaClient client = (OpcUaClient) Client.map.get(urlString).get(0);
				List<NodeId> listNode = OpcUaClentUtils.browseAllNodeId(client, urlString);
				if(listNode!=null && listNode.size()>0) {
					System.out.println(listNode);
					OpcUaDataCache.nodeIds.put(urlString, listNode);
					OpcUaClentUtils.createSubscription(urlString, client, listNode, 50.0);
				}
			}
		});
        es.shutdown();
	}

}
